<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class tree_connect extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tree:connect {uid}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '{uid} 追加树';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $uid = $this->argument('uid');
        $user = DB::table('user_tree')->where('uid',$uid)->first(['uid','left','right','hight']);
        $father = DB::table('user_tree')->where('uid',$user->pid)->first(['uid','tree_id','left','right','hight']);
        if(empty($father->right)){
            die("父还没有设置左右值");
        }
        $bool = DB::table('user_tree')->where('uid',$uid)->update([
            'left'=>$father->right,
            'right'=>$father->right+1,
            'hight'=>$father->hight+1,
            'tree_id'=>$father->tree_id,
        ]);
        if($bool == false){
            exit("error!");
        }
        $this->set_tree($uid);
    }

    public function set_tree($uid){
        $users = DB::table('user_tree')->where([
            ['pid','=',$uid],
            ['tree_id','=',0],
        ])->orderBy('id','asc')->get()->toArray();
        if(empty($users)){
            return true;
        }
        foreach($users as $user){
            $this->set_zy($user->pid,$user->uid);
            $this->set_tree($user->uid);
        }
    }
    
    public function set_zy($pid,$uid){
        $father = DB::table('user_tree')->where('uid',$pid)->first(['tree_id','left','right','hight']);
        $data = [
            'left'=>$father->right,
            'right'=>$father->right+1,
            'hight'=>$father->hight+1,
            'tree_id'=>$father->tree_id,
        ];
        DB::beginTransaction();
        #修改右节点
        $step1 = DB::table('user_tree')->where([
            ['tree_id','=',$father->tree_id],
            ['left','>',$father->right]
        ])->update([
            'left'=>DB::raw("`left`+2"),
            'right'=>DB::raw("`right`+2")
        ]);
        #修改父
        $step2 = DB::table('user_tree')->where([
            ['tree_id','=',$father->tree_id],
            ['left','<=',$father->left],
            ['right','>=',$father->right],
        ])->update([
            'right'=>DB::raw("`right`+2")
        ]);
        #修改自己
        $step3=DB::table('user_tree')->where('uid',$uid)->update($data);
        if($step1!==false && $step2!==false && $step3!==false){
            DB::commit();
            echo "success:{$uid}\n";
            return true;
        }else{
            DB::rollBack();
            echo "fail:{$uid}\n";
            return false;
        }
    }
}
